package com.hzcx.app.simplechat.ui.chat.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyphenate.chat.EMMessage;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseDialog;
import com.hzcx.app.simplechat.ui.publicui.adapter.PublicDialogAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ChatMsgLongClickDialog extends BaseDialog {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.rv_menu)
    RecyclerView rvMenu;
    @BindView(R.id.cons_menu)
    ConstraintLayout consMenu;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;

    private List<String> list;
    private PublicDialogAdapter dialogAdapter;
    private EMMessage message;
    private OnMsgLongClickMenuClickListener listener;

    public ChatMsgLongClickDialog(@NonNull Context context, EMMessage message, OnMsgLongClickMenuClickListener listener) {
        super(context);
        this.message = message;
        this.listener = listener;
    }

    @Override
    public int getLayout() {
        return R.layout.dialog_public_round;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        // 显示在最底部
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        tvCancel.setOnClickListener(view -> dismiss());

        list = new ArrayList<>();
        if (message.direct() == EMMessage.Direct.SEND) {
            list.add("撤回");
            list.add("删除");
            if (message.getType() == EMMessage.Type.TXT || message.getType() == EMMessage.Type.IMAGE) {
                list.add("收藏");
            }
            if (message.getType() == EMMessage.Type.TXT) {
                list.add("复制");
            }
        } else {
            list.add("删除");
            if (message.getType() == EMMessage.Type.TXT || message.getType() == EMMessage.Type.IMAGE) {
                list.add("收藏");
            }
            if (message.getType() == EMMessage.Type.TXT) {
                list.add("复制");
            }
        }

        dialogAdapter = new PublicDialogAdapter(R.layout.rv_item_public_menu, list);
        rvMenu.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        rvMenu.setAdapter(dialogAdapter);
    }

    @Override
    public void initData() {

        dialogAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (listener == null) {
                    dismiss();
                    return;
                }
                switch (list.get(position)) {
                    case "删除":
                        listener.delete(message);
                        break;

                    case "复制":
                        listener.copy(message);
                        break;

                    case "撤回":
                        listener.reCall(message);
                        break;

                    case "收藏":
                        listener.collect(message);
                        break;
                }
                dismiss();
            }
        });

    }

    public interface OnMsgLongClickMenuClickListener {
        void delete(EMMessage message);

        void copy(EMMessage message);

        void collect(EMMessage message);

        void reCall(EMMessage message);
    }
}
